package com.kancy.drools.springbootdrools.runner;

import com.kancy.drools.mould.yearendawards.Input;
import com.kancy.drools.mould.yearendawards.Output;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadLocalRandom;

/**
 * @author kancy
 * @date 2020/1/18 18:49
 **/
@Slf4j
@Component
public class TestRunner implements ApplicationRunner {
    @Autowired
    private KieSession kieSession;
    @Autowired
    private KieContainer kieContainer;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Input input = getInput();
        Output output = new Output();
        kieSession.insert(input);
        kieSession.insert(output);
        kieSession.fireAllRules();
        log.info("年终奖计算输入：{}", input);
        log.info("年终奖计算输出：{}", output);
        log.info("员工 {} 年终考核等级为{}，奖励 {} 个月年终奖金，总金额为：{}。",
                input.getEmployName(), output.getLevel() , output.getMonths(), output.getYearEndAwards());
        this.kieSession.dispose();
    }

    private Input getInput() {
        Input input = new Input();
        input.setEmployName("孙悟空");
        input.setSalary(20000.0);
        input.setScore(ThreadLocalRandom.current().nextInt( 50, 100));
        input.setDiscountRate(1.0);
        return input;
    }
}
